/* $Id: AbstractDataServlet.java 130 2011-08-26 06:58:30Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.servlets;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import alt.djudge.frontend.server.datatypes.AbstractEntry;
import alt.djudge.frontend.server.datatypes.UserEntry;
import alt.djudge.frontend.server.models.CoreInterface;
import alt.djudge.frontend.server.models.UsersModel;
import alt.djudge.frontend.server.utils.Linker;


public abstract class AbstractDataServlet extends AbstractServlet
{
	private static final long serialVersionUID = 1L;
	
	protected abstract CoreInterface<? extends AbstractEntry> getModel();

	protected final CoreInterface<? extends AbstractEntry> model = getModel();

	/* Logged in user */
	protected UserEntry user;
	
	/* URL to return to */
	protected String returnToUrl;
	
	/* Message shown to user */
	protected String msg = "";

	/* Requested action */
	protected String action;
	
	/* Entry ID */
	protected Long id;
	
	protected void processAction(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		// empty here. can be overridden in child classes 
	};
	
	// @returns relative URL
	protected String generateReturnUrl(AbstractEntry entry)
	{
		return Linker.link("");
	}

	protected abstract boolean processEdit(HttpServletRequest req, HttpServletResponse resp);

	protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
	/*	if (!model.canDelete(user))
		{
			resp.sendError(HttpServletResponse.SC_FORBIDDEN, "You are not allowed to delete this page");
		}
		else
		{
			if (model.deleteEntry(id))
			{
				msg = "Entry with id = " + id + " deleted";
			}
			else
			{
				msg = "Error deleting id = " + id;
			}
		}*/
	}
	
	protected void doDeleteReal(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
	/*	if (!model.canDelete(user))
		{
			resp.sendError(HttpServletResponse.SC_FORBIDDEN, "You are not allowed to delete this page");
		}
		else
		{
			if (model.realDeleteEntry(id))
			{
				msg = "Entry with id = " + id + " deleted";
			}
			else
			{
				msg = "Error deleting id = " + id;
			}
		}*/
	}
	
	protected void doCopy(HttpServletRequest req, HttpServletResponse resp)
			throws IOException
	{
		/*if (!model.canCreate(user))
		{
			resp.sendError(HttpServletResponse.SC_FORBIDDEN,
					"You are not allowed to copy this page");
		}
		else
		{
			AbstractEntry entry = model.getEntry(id);
			if (entry != null)
			{
				entry.setId(null);
				entry.makePersistent();
				msg = "Entry with id = " + id + " copied";
			}
			else
			{
				msg = "Entry with id = " + id + " not found";
			}
		}*/
	}

	protected void doEdit(HttpServletRequest req, HttpServletResponse resp)
			throws IOException
	{
	/*	if (!model.canEdit(user))
		{
			resp.sendError(HttpServletResponse.SC_FORBIDDEN, "You are not allowed to edit this page");
		}
		else
		{
			if (processEdit(req, resp))
			{
				msg = "Entry edited";
			}
			else
			{
				msg = "Error editing entry";
			}
		}*/
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		req.setCharacterEncoding("UTF-8");
		
		/* Setting params */
		user = UsersModel.getCurrentUser(req);
		returnToUrl = req.getParameter("return");
		action = req.getParameter("action");
		try
		{
			id = Long.parseLong(req.getParameter("id"));
		}
		catch (Exception e)
		{
			id = 0L;
		}
		
		if ("delete".equalsIgnoreCase(action))
		{
			doDelete(req, resp);
		}
		else if ("deletepers".equalsIgnoreCase(action))
		{
			doDeleteReal(req, resp);
		}
		else if ("edit".equalsIgnoreCase(action))
		{
			doEdit(req, resp);
		}
		else if ("copy".equalsIgnoreCase(action))
		{
			doCopy(req, resp);
		}
		else
		{
			processAction(req, resp);
		}
		
		if (returnToUrl == null || returnToUrl.equals("null"))
			returnToUrl = Linker.link("");

		String finalUrl = null;
		if (!returnToUrl.startsWith("http://"))
			finalUrl = Linker.link(returnToUrl.substring(Linker.link("").length()));
		else
			finalUrl = returnToUrl;
		if (msg != null && msg.length() > 0)
			finalUrl = finalUrl + (finalUrl.contains("?") ? "&" : "?") + "msg=" + URLEncoder.encode(msg, defaultEncoding);
		resp.sendRedirect(finalUrl);
	}
	
	protected void setRedirectAbsolute(String absoluteUrl)
	{
		returnToUrl = absoluteUrl;
	}
	
	protected void setRedirect(String relativeUrl)
	{
		relativeUrl = Linker.link(relativeUrl);
	}
	
	protected void setMessage(String msg)
	{
		this.msg = msg;
	}
}


